package com.spring.cloud.filtr;

import com.spring.cloud.vo.RequestProxy;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 替换Request对象过滤器
 *
 * 将所有的Request替换成自定义的RequestProxy
 *
 */

public class RequestProxyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        ServletRequest requestProxy = null;
        if(request instanceof HttpServletRequest) {
            requestProxy = new RequestProxy((HttpServletRequest) request);
        }
        if(requestProxy == null) {
            filterChain.doFilter(request, response);
        } else {
            filterChain.doFilter(requestProxy, response);
        }
    }

    @Override
    public void destroy() {}
}
